<template>
  <div>
    <el-upload
      class="avatar-uploader"
      :action="src"
      list-type="picture-card"
      :show-file-list="false"
      :on-success="handleSuccess"
      :on-remove="handleRemove"
      :before-upload="beforeAvatarUpload"
    >
      <div v-if="imagesUrl" slot="tip" class="fileEdit">
        <span @click="handleRemove">
          <i class="el-icon-delete" />
        </span>
      </div>
      <img v-if="imagesUrl" :src="imagesUrl" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
  </div>
</template>

<script>
import { sysAjax } from "Ajax";
import axios from 'axios'
export default {
  props: ['value','srcDateImg'],
  // {
  //   : {
  //     type: String,
  //     default: function() {
  //       return ''
  //     }
  //   }
  // },

  data() {
    return {
      imagesUrl: '',
      src:  process.env.VUE_APP_BASE_URL + 'api/oss/policy'
    }
  },
  watch: {
    value: {
      immediate: true,
      handler: function(value) {
        if (!value) {
          this.fileList = []
          this.imagesUrl = ''
          return
        }
        this.imagesUrl = value
      }
    }
  },
  mounted() {
  },
  methods: {
    handleRemove(file) {
      this.fileList = []
      this.imagesUrl = ''
    },
    // 上传成功
    handleSuccess(res, file , fileList) {
      this.imagesUrl = res.data.url
      let obj = {}
      // 图片路径返回给父组件
    },
    // 上传成功之前 对图片格式要求
    beforeAvatarUpload(file) {
      const isPNG = file.type === 'image/png'
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isPNG && !isJPG) {
        this.$message.error('上传图片只能是 JPG 格式和PNG格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      const index = file.name.indexOf('.')
      const nameEnd = file.name.substr(index)
      const nameStart = new Date().getTime() + '' + Math.ceil(Math.random() * 100)
      const name = '/' + nameStart + nameEnd
      sysAjax.getOssPolicy().then((res)=>{
        const sendData = new FormData()
        sendData.append('OSSAccessKeyId', res.accessKeyId)
        sendData.append('policy', res.policy)
        sendData.append('signature', res.signature)
        sendData.append('key', res.dir + name)
        sendData.append('success_action_status', '200') // 指定返回的状态码
        sendData.append('callback', res.callback)
        sendData.append('type', 'image/jpeg')
        sendData.append('file', file)
        axios.post(res.host, sendData, {
          'Content-Type': 'multipart/form-data'
        }).then(ossRes => {
          this.imagesUrl = ossRes.data.data.filename
          let obj = {
            url: ossRes.data.data.filename,
            type: this.srcDateImg
          }
          this.$emit('input', obj)
        })
      })
      return false
    }
  }
}
</script>

<style>
.fileEdit {
  position: absolute;
  right: 10px;
  top: 0;
}
.el-icon-download {
  cursor: pointer;
  color: red;
  font-size: 20px;
}
.el-icon-delete {
  cursor: pointer;
  color: red;
  font-size: 20px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 160px;
  height: 140px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
/*   
  line-height: 178px;
  text-align: center; */
}
.avatar {
  width: 100%;
  height: 100%;
  display: block;
}
</style>
